<?php
/***********************************************************
	Filename: app/admin/global.func.php
	Note	: 后台公共函数
	Version : 3.0
	Author  : qinggan
	Update  : 2009-10-17
***********************************************************/
if(!function_exists("error"))
{
	function error($msg="",$url="",$time=2)
	{
		$app = sys_init();
		//哪果没有内容
		if(!$msg && !$url)
		{
			exit("error: false");
		}
		//如果没有内容提示，则直接跳转
		if(!$msg)
		{
			sys_header($url);
		}
		//如果有内容提示跳转
		$app->tpl->assign("msg",$msg);
		$app->tpl->assign("error_url",$url);
		if($url)
		{
			$error_note = sys_eval($app->lang["error_note"],$time);
			$app->tpl->assign("error_note",$error_note);
		}
		$app->tpl->assign("time",$time);
		//毫秒级，在JS中应用
		$app->tpl->assign("micro_time",$time*1000);
		$app->tpl->display("error.".$app->tpl->ext);
		exit();
	}
}

//rs是数组，根据数组原则生成静态页及伪静态页链接
function msg_url($rs,$format=true)
{
	$app = sys_init();
	if(!$rs)
	{
		return $app->url();
	}
	if(!$format || ($app->sys_config["site_type"] != "html" && $app->sys_config["site_type"] != "rewrite"))
	{
		return $app->url("msg","id=".$rs["id"]);
	}
	if($app->sys_config["site_type"] == "html" && !$app->sys_config["sitehtml"])
	{
		$app->sys_config["site_type"] = "rewrite";
	}
	if($app->sys_config["site_type"] == "rewrite")
	{
		$url = "c".$rs["id"].".html";
		if($rs["identifier"])
		{
			$url = $rs["identifier"].".html";
		}
		return $url;
	}
	//格式化网址
	$url = $app->sys_config["sitehtml"];
	if(substr($url,-1) != "/")
	{
		$url .= "/";
	}
	$this_url_end = ($rs["identifier"] && $rs["identifier"] != "index") ? $rs["identifier"].".html" : "c".$rs["id"].".html";
	if($rs["htmltype"] == "root")
	{
		$url .= $this_url_end;
	}
	elseif($rs["htmltype"] == "mid" || $rs["htmltype"] == "cateid")
	{
		if(!$app->module_m)
		{
			$app->load_model("module");
			$_tmp_rs = $APP->module_m->get_id_code_list();
			if($_tmp_rs)
			{
				$app->module_codelist = $_tmp_rs["code"];
				$app->module_idlist = $_tmp_rs["id"];
				unset($_tmp_rs);
			}
		}
		$url .= $app->module_idlist[$rs["module_id"]]."/";
		if($rs["htmltype"] == "cateid" && $rs["cate_id"])
		{
			$app->load_model("cate");
			if(!$app->cate_m)
			{
				$_tmp_rs = $APP->cate_m->get_id_code_list();
				if($_tmp_rs)
				{
					$app->cate_codelist = $_tmp_rs["code"];
					$app->cate_idlist = $_tmp_rs["id"];
					unset($_tmp_rs);
				}
			}
			$url .= $app->cate_idlist[$rs["cate_id"]]."/";
		}
		$url .= $this_url_end;
	}
	else
	{
		$url .= date("Ym/d/",$rs["post_date"]);
		$url .= $this_url_end;
	}
	return $url;
}

function list_url($rs,$pageid=0,$format=true,$ifindex=true)
{
	$app = sys_init();
	if(!$rs)
	{
		return $app->url();
	}
	if(!$format || ($app->sys_config["site_type"] != "html" && $app->sys_config["site_type"] != "rewrite"))
	{
		return $app->url("list","cid=".$rs["id"]);
	}
	if($app->sys_config["site_type"] == "html" && !$app->sys_config["sitehtml"])
	{
		$app->sys_config["site_type"] = "rewrite";
	}
	if($app->sys_config["site_type"] == "rewrite")
	{
		$url = $rs["identifier"] ? "cs-".$rs["identifier"] : "cid-".$rs["id"];
		$url.= $pageid<2 ? ".html" : "-".$pageid.".html";
		return $url;
	}
	//针对HTML的格式化
	$url = $app->sys_config["sitehtml"];
	if(substr($url,-1) != "/")
	{
		$url .= "/";
	}
	if(!$app->module_m)
	{
		$app->load_model("module");
		$_tmp_rs = $APP->module_m->get_id_code_list();
		if($_tmp_rs)
		{
			$app->module_codelist = $_tmp_rs["code"];
			$app->module_idlist = $_tmp_rs["id"];
			unset($_tmp_rs);
		}
	}
	$url .= $app->module_idlist[$rs["module_id"]] . "/";
	if(!$app->cate_m)
	{
		$app->load_model("cate");
		if(!$app->cate_m)
		{
			$_tmp_rs = $APP->cate_m->get_id_code_list();
			if($_tmp_rs)
			{
				$app->cate_codelist = $_tmp_rs["code"];
				$app->cate_idlist = $_tmp_rs["id"];
				unset($_tmp_rs);
			}
		}
	}
	$url .= $app->cate_idlist[$rs["id"]]."/";
	if($pageid<2)
	{
		if($ifindex)
		{
			$url .= "index.html";
		}
	}
	else
	{
		$url .= $pageid.".html";
	}
	return $url;
}

if(!function_exists("site_url"))
{
	function site_url($value,$extend="",$format=true)
	{
		$app = sys_init();
		if(!$value)
		{
			return $app->url();
		}
		if(!$format || ($app->sys_config["site_type"] != "html" && $app->sys_config["site_type"] != "rewrite"))
		{
			return $app->url($value,$extend);
		}
		if($app->sys_config["site_type"] == "html" && !$app->sys_config["sitehtml"])
		{
			$app->sys_config["site_type"] = "rewrite";
		}
		$format_type = $app->sys_config["site_type"];
		//切分地址
		$ext = explode(",",$value);
		$c = $ext[0];
		if(!$c)
		{
			return false;
		}
		if($c == "js")
		{
			return $app->url($value,$extend);
		}
		$f = $ext[1] ? $ext[1] : "";
		$d = $ext[2] ? $ext[2] : "";
		if($extend)
		{
			$array = explode("&",$extend);
			foreach($array AS $k=>$v)
			{
				$tmp_array = explode("=",$v);
				$$tmp_array[0] = $tmp_array[1];
			}
		}
		if($c == "list" && $extend)
		{
			if($cid || $cs)
			{
				if($format_type == "rewrite")
				{
					$url = $cid ? "cid-".$cid : "cs-".$cs;
					if($pageid && $pageid>1)
					{
						$url .= "-".$pageid;
					}
					$url .= ".html";
					return $url;
				}
				else
				{
					$app->load_model("cate");
					if(!$cid && $cs)
					{
						$cid = $app->cate_m->get_cid_from_code($cs);
					}
					if($cid)
					{
						$rs = $app->cate_m->get_one($cid);
						return list_url($rs);
					}
				}
			}
			else
			{
				if($format_type == "rewrite")
				{
					$url = $mid ? "mid-".$mid : "ms-".$ms;
					if($pageid && $pageid>1)
					{
						$url .= "-".$pageid;
					}
					$url .= ".html";
					return $url;
				}
				else
				{
					$app->load_model("module");
					if($ms && !$mid)
					{
						$mid = $app->module_m->get_mid_from_code($ms);
					}
					if($mid)
					{
						$rs = $app->module_m->get_one($mid);
						return list_url($rs);
					}
				}
			}
		}
		elseif($c == "msg" && $extend)
		{
			if($format_type == "rewrite")
			{
				$url = $id ? "c".$id.".html" : $ts.".html";
				return $url;
			}
			else
			{
				$app->load_model("msg");
				if($id)
				{
					$rs = $app->msg_m->get_one($id);
				}
				else
				{
					$rs = $app->msg_m->get_one_fromtype($ts,$_SESSION["sys_lang_id"]);
				}
				return msg_url($rs);
			}
		}
		return $app->url($value,$extend);
	}
}

if(!function_exists("sys_format_list"))
{
	function sys_format_list($val,$input)
	{
		$app = sys_init();
		$app->load_model("format");
		return $app->format_m->format($val,$input);
	}
}

if(!function_exists("sys_format_content"))
{
	function sys_format_content($msg)
	{
		if(!$msg)
		{
			return false;
		}
		$list = array();
		preg_match_all("/\[((download|video):([0-9]+))\]/isU",$msg,$list);
		$list = array_unique($list[1]);
		if(!$list || !is_array($list) || count($list)<1)
		{
			return $msg;
		}
		$app = sys_init();
		$app->load_model("upfile");
		if(!$app->lang["download"])
		{
			$app->lang["download"] = "download";
		}
		foreach($list AS $key=>$value)
		{
			$array = explode(":",$value);
			if(!$array[1])
			{
				continue;
			}
			if(!$array[0])
			{
				$array[0] = "download";
			}
			$type = in_array($array[0],array("download","video")) ? $array[0] : "download";
			$id = $array[1];
			$rs = $app->upfile_m->get_one($id);
			if(!$rs)
			{
				continue;
			}
			if($type == "video")
			{
				$n_msg = "<div class='video'>";
				$n_msg.= "<script type='text/javascript'>";
				$n_msg.= 'var htmlmsg = Media.init("'.$rs["filename"].'","546","350");';
				$n_msg.= "document.write(htmlmsg);</script>";
				$n_msg.= "</div>";
			}
			else
			{
				$n_msg = "<div class='download'><a href='".site_url("download","id=".$id)."'>";
				$n_msg.= "<img src='images/download.gif' align='absmiddle'> ";

				$n_msg.= $app->lang["download"].": ".$rs["title"];
				$n_msg.= "</a></div>";
			}
			$msg = str_replace("[".$value."]",$n_msg,$msg);
		}
		return $msg;
	}
}
?>